﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Utilities;

namespace BusinessLogicLayer
{
    /// <summary>
    /// Get state of HOSO
    /// </summary>
    public static class TrangThaiHoSo
    {
        public static readonly string DA_PV = "Đã phỏng vấn";
        public static readonly string CHOXETDUYET = "Chờ xét duyệt";
        public static readonly string NHAN_LV = "Nhận làm việc";
    }

    /// <summary>
    /// 
    /// </summary>
    public class HoSoUngVienManager
    {
        /// <summary>
        /// Get all list of table HoSoUngVien
        /// </summary>
        /// <returns></returns>
        public ActionResultAsList<HoSoUngVien> LayDS()
        {
            var ar = new ActionResultAsList<HoSoUngVien>();
            try
            {
                using (var db = new QLNVDbContext())
                    ar.ObjectList = db.HoSoUngViens.Include("DuAnQuaKhus").Include("NhanVien").ToList();
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }


        /// <summary>
        /// Get list HoSoUngVien with state "Chờ Xét Duyệt"
        /// </summary>
        /// <returns></returns>
        public ActionResultAsList<HoSoUngVien> LayDSCho()
        {
            var ar = new ActionResultAsList<HoSoUngVien>();
            var list = this.TimTheoTrangThai("Chờ xét duyệt");
            if (list.Success)
                ar.ObjectList = list.ObjectList;
            else ar.Exceptions.AddRange(list.Exceptions);
            return ar;
        }

        /// <summary>
        /// Add one record HoSoUngVien to table HoSoUngVien
        /// </summary>
        /// <param name="hoSoUV"></param>
        /// <returns></returns>
        public ActionResult Them(HoSoUngVien hoSoUV)
        {
            var ar = new ActionResult();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    var ar1 = this.CheckHoSo(hoSoUV);
                    if (ar1.Success)
                    {
                        db.HoSoUngViens.Add(hoSoUV);
                        db.SaveChanges();
                    }
                    else ar.Exceptions.AddRange(ar1.Exceptions);
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }
            }
            return ar;
        }


        public HoSoUngVien LayHSTheoMa(int maHS)
        {
            HoSoUngVien hs = new HoSoUngVien();
            using (var db = new QLNVDbContext())
            {
                hs = db.HoSoUngViens.Include("DuAnQuaKhus").Include("NhanVien").Single(h => h.MaHoSo == maHS);
            }
            return hs;
        }
        public ActionResult CapNhatPhongVan(int maHs, string maPV)
        {
            var ar = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var hs = db.HoSoUngViens.Single(h => h.MaHoSo == maHs);
                    hs.NhanVienPhongVan = maPV;
                    hs.TrangThai = TrangThaiHoSo.DA_PV;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResultAsList<NhanVien> LayDSNVPhongVan()
        {
            var ar = new ActionResultAsList<NhanVien>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var list = db.NhanViens.Where(n => n.ChucVu == "LEAD");
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        private ActionResult CheckHoSo(HoSoUngVien hoso)
        {
            ActionResult ar = new ActionResult();
            if (!CMND_Validate(hoso.CMND))
                ar.AddException(new Exception("Chứng minh nhân dân không hợp lệ!!!"));
            if (!Email_Validate(hoso.Email))
                ar.AddException(new Exception("Địa chỉ Email không hợp lệ!!!"));
            return ar;
        }

        /// <summary>
        /// Check validate of CMND
        /// </summary>
        /// <param name="cmnd"></param>
        /// <returns></returns>
        private bool CMND_Validate(string cmnd)
        {
            int i;
            if (int.TryParse(cmnd, out i))
                return true;
            return false;
        }

        /// <summary>
        /// Check validate of Email
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        private bool Email_Validate(string email)
        {
            bool isEmail = Regex.IsMatch(email, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase);
            return isEmail;
        }

        /// <summary>
        /// Delete one record HoSoUngVien from table HoSoUngVien
        /// </summary>
        /// <param name="maHoSo"></param>
        /// <returns></returns>
        public ActionResult Xoa(int maHoSo)
        {
            var ar = new ActionResult();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    var hoSo = db.HoSoUngViens.Single(hs => hs.MaHoSo == maHoSo);
                    db.HoSoUngViens.Remove(hoSo);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }
            }
            return ar;
        }

        /// <summary>
        /// Update table HoSoUngVien
        /// </summary>
        /// <param name="hoSoUV"></param>
        /// <returns></returns>
        public ActionResult Sua(HoSoUngVien hoSoUV)
        {
            var ar = new ActionResult();
            var ar1 = this.CheckHoSo(hoSoUV);
            if (ar1.Success)
            {
                using (var db = new QLNVDbContext())
                {
                    try
                    {
                        var hoSo = db.HoSoUngViens.Single(hs => hs.MaHoSo == hoSoUV.MaHoSo);
                        hoSo.CMND = hoSoUV.CMND;
                        hoSo.DiaChi = hoSoUV.DiaChi;
                        hoSo.Email = hoSoUV.Email;
                        hoSo.GioiTinh = hoSoUV.GioiTinh;
                        hoSo.HoUngVien = hoSoUV.HoUngVien;
                        hoSo.KinhNghiem = hoSoUV.KinhNghiem;
                        hoSo.KyNangChuyenMon = hoSoUV.KyNangChuyenMon;
                        hoSo.KyNangNgoaiNgu = hoSoUV.KyNangNgoaiNgu;
                        hoSo.NgaySinh = hoSoUV.NgaySinh;
                        hoSo.SoDienThoai = hoSoUV.SoDienThoai;
                        hoSo.TenUngVien = hoSoUV.TenUngVien;
                        hoSo.TrinhDoHocVan = hoSoUV.TrinhDoHocVan;
                        hoSo.ViTriMongMuon = hoSoUV.ViTriMongMuon;
                        db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        Logger.Write(ex);
                        ar.AddException(ex);
                    }
                }
            }
            else ar.Exceptions.AddRange(ar1.Exceptions);
            return ar;
        }

        public ActionResult CapNhatTrangThai(int maHS, string trangThai)
        {
            var ar = new ActionResult();
            using (var db = new QLNVDbContext())
            {
                try
                {
                    var hoSo = db.HoSoUngViens.Single(hs => hs.MaHoSo == maHS);
                    hoSo.TrangThai = trangThai;
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }
            }
            return ar;
        }

        /// <summary>
        /// Search list of HoSoUngVien with search criterion
        /// </summary>
        /// <param name="trinhDoHV"></param>
        /// <param name="vTMongMuon"></param>
        /// <param name="KyNang"></param>
        /// <param name="Ten"></param>
        /// <returns></returns>
        public ActionResultAsList<HoSoUngVien> TimTheoTieuChi(string trinhDoHV, string vTMongMuon, string KyNangNN, string KyNangCM, string Ten, string TrangThai)
        {
            var ar = new ActionResultAsList<HoSoUngVien>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var list = db.HoSoUngViens.Include("DuAnQuaKhus").Include("NhanVien").Where(h => h.TrinhDoHocVan.Contains(trinhDoHV)
                        && h.ViTriMongMuon.Contains(vTMongMuon)
                        && h.KyNangChuyenMon.Contains(KyNangCM)
                        && h.KyNangNgoaiNgu.Contains(KyNangNN)
                        && h.TenUngVien.Contains(Ten)
                        && h.TrangThai.Contains(TrangThai));
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        /// <summary>
        /// Search list of HoSoUngVien with state
        /// </summary>
        /// <param name="trangThai"></param>
        /// <returns></returns>
        public ActionResultAsList<HoSoUngVien> TimTheoTrangThai(String trangThai)
        {
            var ar = new ActionResultAsList<HoSoUngVien>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var list = db.HoSoUngViens.Include("DuAnQuaKhus").Include("NhanVien").Where(h => h.TrangThai == trangThai);
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
    }
}
